﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace App
{
    public partial class UserStatistics : System.Web.UI.Page
    {
        AppLib.User user;
        AppLib.Team team;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Common.GetLoggedUser(this.Page) == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }

            int aux = int.Parse(Server.HtmlEncode(Request.Cookies["UserDetail"].Value));
            user = MidApp.ManageUser.GetUserByID(aux);
            ImageAvatar2.DefaultImage = user.ImageURL;
            ImageAvatar2.Email = user.PrimaryEmail;
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            List<AppLib.Team> teams = new List<AppLib.Team>(MidApp.ManageTeam.GetTeamsWhereUserIsMember(user.UserID));
            teams.AddRange(MidApp.ManageTeam.GetTeamsWhereUserIsLeader(user.UserID));
            RepeaterTeamMember.DataSource = teams;
            RepeaterTeamMember.DataBind();            

            LabelNick.Text = user.Nick;
        }
        protected List<AppLib.IsStockDetail> DataSource(object sender)
        {
            var aux = team = (AppLib.Team)sender;

            return MidApp.ManageIsStockDetail.GetAllUserDetails(user.UserID, aux.TeamID);
        }
        protected string GetTeamAvatar(object sender)
        {
            var aux = (AppLib.Team)sender;

            return aux.ImageURL;
        }
        protected string GetTeamID(object sender)
        {
            var aux = (AppLib.Team)sender;

            return aux.TeamID.ToString();
        }
        protected string GetTeamName(object sender)
        {
            var aux = (AppLib.Team)sender;

            return aux.TeamName;
        }
        protected string GetUserCredit(object sender)
        {
            var aux = (AppLib.IsStockDetail)sender;

            return MidApp.ManageDetail.GetAmount(aux.DetailID);
        }
        protected string GetCreditDescription(object sender)
        {
            var aux = (AppLib.IsStockDetail)sender;

            return MidApp.ManageDetail.GetDescription(aux.DetailID);
        }
        protected string GetCreditDate(object sender)
        {
            var aux = (AppLib.IsStockDetail)sender;
            

            return MidApp.ManageDetail.GetDate(aux.DetailID);
        }
        protected string GetMemberTotalCredit()
        {
            var aux = MidApp.ManageStock.GetPlayer(team.TeamID, user.UserID).Credit;
            return aux.ToString();
        }
        protected string GetMemberPlusCredits()
        {
            var list = MidApp.ManageIsStockDetail.GetAllUserDetails(user.UserID, team.TeamID);
            int total = 0;
            foreach (var item in list)
            {
                string amount = MidApp.ManageDetail.GetAmount(item.DetailID);
                if (!amount.StartsWith("-"))
                {
                    total = total + int.Parse(amount);
                }
            }
            return total.ToString();
        }
        protected string GetMemberMinusCredits()
        {
            var list = MidApp.ManageIsStockDetail.GetAllUserDetails(user.UserID, team.TeamID);
            int total = 0;
            foreach (var item in list)
            {
                string amount = MidApp.ManageDetail.GetAmount(item.DetailID);
                if (amount.StartsWith("-"))
                {
                    total = total + int.Parse(amount);
                }
            }
            return total.ToString();
        }
        protected void LinkButtonTeamInfo_Click(object sender, EventArgs e)
        {
            Response.Cookies["TeamDetail"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("TeamDetails.aspx");
        }
        protected void LinkButtonDeleteUserStatistics_Click(object sender, EventArgs e)
        {
            var teamID = int.Parse(((LinkButton)sender).CommandArgument);

            List<AppLib.IsStockDetail> list = MidApp.ManageIsStockDetail.GetAllUserDetails(user.UserID, teamID);

            foreach (var item in list)
            {
                MidApp.ManageIsStockDetail.DeleteDetail(item.DetailID, item.TeamID, item.UserID);
                MidApp.ManageDetail.DeleteDetail(item.DetailID);
            }
            MidApp.ManageStock.ChangeCreditsBalanceToZero(user.UserID, teamID);
        }
        protected bool IsUserTreasurer(object sender)
        {
            var teamID = ((AppLib.Team)sender).TeamID;

            return MidApp.ManageIsMember.IsTeamTreasurer(teamID, Common.GetLoggedUser(this.Page).UserID);
        }
    }
}